<%@page import="com.office.automate.configuration.property.DatagridPageConfiguration"%>
<%@page import="com.office.automate.model.leave.LeaveRequest"%>
<%@page import="com.office.automate.service.def.leave.EmployeeLeaveService"%>
<%@page import="com.office.automate.service.impl.leave.EmployeeLeaveServiceImplementer"%>
<%@page import="com.office.automate.model.leave.EmployeeLeave"%>
<%@page import="com.office.automate.configuration.property.ConfigurationProperties"%>
<%@page import="java.util.Properties"%>
<%@page import="java.util.List"%>
<%@page import="com.office.automate.frontier.utility.DataConversionUtility"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.office.automate.frontier.utility.ExceptionMessagesManagement"%>
<%@page import="com.office.automate.validation.framework.ValidatorObject"%>
<%@page import="com.office.automate.exception.framework.FrontierVisibleException"%>
<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ include file="../common/CommonHeaderInfo.jsp"%>
<title>Employee Leave</title>
</head>
<body>
	<%@ include file="../common/SiteHeaderSection.jsp" %>
	<%@ include file="../common/LeaveManagementMenu.jsp"%>

		<%
			if (currentUser == null) {
				return;
			}
			Properties configSeqPropspg = ConfigurationProperties.getProps(); 
			Utility utilpg = new Utility(currentUser.getSecurityLevels());
			if (!utilpg.isFunctionalSecurityAllow(configSeqPropspg.getProperty("MAIN_LEAVE_MANAGEMENT")) ||  
						!utilpg.isFunctionalSecurityAllow(configSeqPropspg.getProperty("APPLY_LEAVE")))  {
				response.sendRedirect("/OfficeAutomation/jsp/HomePage.jsp");
			}
			boolean isValidationErrorExist = false;
			boolean isLeaveEnable = true;
			String aDataGridPageID = DatagridPageConfiguration.EMPLOYEE_LEAVE_DETAILS;
			String aNextDataRowNumber = (String)request.getAttribute("NextDataRowNumber");
			ExceptionMessagesManagement exceptionMessagesManagement  = null;
			Properties configProps = ConfigurationProperties.getProps();
			DataConversionUtility dataConversionUtility = new DataConversionUtility();
			EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();
			int year=Calendar.getInstance().get(Calendar.YEAR);
			EmployeeLeave employeeLeave = new EmployeeLeave();
			long loggedInUserId = 0;
			if (currentUser != null) {
				loggedInUserId= currentUser.getEmployeeId();
				employeeLeave = employeeLeaveService.getCurrentEntitlementBalance(year, loggedInUserId);			
			}
			ValidatorObject validationErrors = (ValidatorObject) request.getAttribute("FieldValidationErrors");
			LeaveRequest leaveRequest = (LeaveRequest)request.getAttribute("LeaveRequest");
		%>
		<tr>
		<td>
		<form action="/OfficeAutomation/RequestLeaveServlet" method="post">
		<center>
		<table>
			<tr>
				<td colspan="3"><center><font class="formtablecaption"><u>Employee Leave</u></font></center><br /></td>
			</tr>
			<tr>
			<%
			if (validationErrors != null) {
				exceptionMessagesManagement = new ExceptionMessagesManagement(validationErrors.getValidationErrorList());
				isValidationErrorExist = true;
			}
			%>
				  <td>
				  <center>
						<table class="mainContentStyles">
							<tr>
								<td>
									<center><h5><u>Employee Current Leave Balance</u></h5></center>
									<table class="datagridstyle">
										<tr class="grid-heding-row-style">
											<th class="grid-heding-cell-style">Annual Leave</th>
											<th class="grid-heding-cell-style">Casual Leave</th>
											<th class="grid-heding-cell-style">Medical Leave</th>
											<th class="grid-heding-cell-style">Lieu Leave</th>
											<th class="grid-heding-cell-style">Maternity/Paternity Leave</th>
										</tr>
										<% if (employeeLeave != null) { %>
										<tr class="data-row-even-style">
											<td class="data-row-cell-style"><%=employeeLeave.getAnualLeaveCount() %> </td>
											<td class="data-row-cell-style"><%=employeeLeave.getCasualLeaveCount() %> </td>
											<td class="data-row-cell-style"><%=employeeLeave.getMedicalLeaveCount() %> </td>
											<td class="data-row-cell-style"><%=employeeLeave.getLieuLeaveCount() %> </td>
											<td class="data-row-cell-style"><%=employeeLeave.getMaternityOrPaternityLeaveCount() %> </td>
										</tr>
										<%
										} else {
											isLeaveEnable = false;
										%>
											<tr class="grid-no_data-message-row-style">
												<td colspan="5">
													<%=configProps.getProperty("SYSTEM_DATAGRID_NO_DATA_MESSAGE_TEXT") %>
												</td>
											</tr>
										<% }%>
									</table>
								</td>
							</tr>
						</table>
						<br />
						<table>
							<tr>
								<td width="200px"> Leave Type </td>
								<td>
									<select name="leaveType" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>>
										<option />
										<option value="Annual" <% if (isValidationErrorExist && leaveRequest.getLeaveType() != null && leaveRequest.getLeaveType().equalsIgnoreCase("Annual")) { %> selected="selected" <% } %>>Annual Leave</option>
										<option value="Casual" <% if (isValidationErrorExist && leaveRequest.getLeaveType() != null && leaveRequest.getLeaveType().equalsIgnoreCase("Casual")) { %> selected="selected" <% } %>>Casual Leave</option>
										<option value="Medical" <% if (isValidationErrorExist && leaveRequest.getLeaveType() != null && leaveRequest.getLeaveType().equalsIgnoreCase("Medical")) { %> selected="selected" <% } %>>Medical Leave</option>
										<option value="Lieu" <% if (isValidationErrorExist && leaveRequest.getLeaveType() != null && leaveRequest.getLeaveType().equalsIgnoreCase("Lieu")) { %> selected="selected" <% } %>>Lieu Leave</option>
										<option value="MatOrPat" <% if (isValidationErrorExist && leaveRequest.getLeaveType() != null && leaveRequest.getLeaveType().equalsIgnoreCase("MatOrPat")) { %> selected="selected" <% } %>>Maternity/Paternity Leave</option>
									</select>
								</td>
							</tr>
							<%
							 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("LeaveType")) {
							%>
								<tr>
									<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("LeaveType")  %> </font></td>
								</tr>
							<%
							 }
							%>
							<tr>
								<td>From Date </td>
								<td>
								  <select name="fromday" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>>
								  	<%
								  		for (int i=1; i<=31; i++) {
									%>
										<option value="<%=i %>" <% if (isValidationErrorExist && i == leaveRequest.getFromDate().getDate()) { %> selected="selected" <% } %>><%=i %></option>
									<%
								  		}
								  	%>
								 </select>/
								<select name="frommonth" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>>
										<%

											for (int i=0; i<12; i++) {
										%>
												<option value="<%=i+1 %>" <% if (isValidationErrorExist && i == leaveRequest.getFromDate().getMonth()) { %> selected="selected" <% } %>><%=dataConversionUtility.getMonthDescription(i) %></option>
										<%
											}
										%>
	  							  </select>/
	  							  <%=year %>
	  							  	<input type="hidden" name="fromyear" value="<%=year %>">
								</td>
							</tr>
							<%
							 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("FromDate")) {
							%>
								<tr>
									<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("FromDate")  %> </font></td>
								</tr>
							<%
							 }
							%>
							<tr>
								<td>To Date </td>
								<td>
								  <select name="today" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>>
								  	<%
								  		for (int i=1; i<=31; i++) {
									%>
										<option value="<%=i %>" <% if (isValidationErrorExist && i == leaveRequest.getToDate().getDate()) { %> selected="selected" <% } %>><%=i %></option>
									<%
								  		}
								  	%>
								 </select>/
								<select name="tomonth" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>>
										<%

											for (int i=0; i<12; i++) {
										%>
												<option value="<%=i+1 %>" <% if (isValidationErrorExist && i == leaveRequest.getToDate().getMonth()) { %> selected="selected" <% } %>><%=dataConversionUtility.getMonthDescription(i) %></option>
										<%
											}
										%>
	  							  </select>/
	  							  <%=year %>
	  							  	<input type="hidden" name="toyear" value="<%=year %>">
								</td>
							</tr>
							<%
							 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("ToDate")) {
							%>
								<tr>
									<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("ToDate")  %> </font></td>
								</tr>
							<%
							 }
							%>
							<tr>
								<td>Reason</td>
								<td><textarea name="reason" cols="30" rows="5" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>><% if (isValidationErrorExist) { %><%=leaveRequest.getReason() %> <% } %></textarea></td>
							</tr>
							<%
							 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("Reason")) {
							%>
								<tr>
									<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("Reason")  %> </font></td>
								</tr>
							<%
							 }
							%>
						</table>
					</center>
					</td>
				</tr>
				<tr>
					<td colspan="3"><div class="formButtonPanel"><input type="submit" name="submit" value="Submit" <% if (!isLeaveEnable) { %>disabled="disabled" <% } %>/></div></td>
				</tr>
			</table>
		  </center>
		</form>
	  </td>
	</tr>
		<tr>
			<td height="20px">
			
			</td>
		</tr>
		<tr>
			<td>
				<center>
				        <jsp:include page="../frameworks/DataGridComponentLeaveManagement.jsp">
			            	<jsp:param name="aDataGridPageID" value="<%=aDataGridPageID %>" />
			            	<jsp:param name="aNextRowNumber" value="<%=aNextDataRowNumber %>" />
			            	<jsp:param name="aCurrentUser" value="<%=loggedInUserId %>" />
			            	<jsp:param name="aButtonDeleteCaption" value="Cancel" />
			            	<jsp:param name="aButtonsNextPrevious" value="true" />
			        	</jsp:include>
				</center>
			</td>
		</tr>
<%@ include file="../common/SiteFotterSection.jsp" %>
</body>
</html>